<?php

class AdminController extends AdminhtmlController
{
	public $layout='application.modules.adminhtml.views.layouts.column2';
	
	/* this function must write final controller file!! */
	protected function getActionsKey()
	{
		return __CLASS__;
	}
	
	public function init()
	{
		parent::init();
		$this->menu=array(
			1=> array('label'=>'管理记录 [ Grid ]', 'url'=>array('admin')),
			//2=> array('label'=>'数据列表 [ List ]', 'url'=>array('index')),
			3=> array('label'=>'新建数据 [ Create ]', 'url'=>array('create')),
		);
		$this->controllerLable = '后台管理员';
	}
	
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->setUnionTitle('查看信息#'. $id);
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$this->setUnionTitle('创建管理员');
		$model=new AdminhtmlAdmin;
		$this->attachLogBehavior($model);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['AdminhtmlAdmin']))
		{
			if($_POST['AdminhtmlAdmin']['password']!= $_POST['AdminhtmlAdmin']['password_cf']) {
				Yii::app()->admin->putErrorMsg('两次密码输入不一致！');
				
			} elseif (isset($_POST['AdminhtmlAdmin']['password']) && $_POST['AdminhtmlAdmin']['password']!='' ) {
				$_POST['AdminhtmlAdmin']['password']= md5($_POST['AdminhtmlAdmin']['password']);
				$_POST['AdminhtmlAdmin']['password_cf']= md5($_POST['AdminhtmlAdmin']['password_cf']);
				
			}
			$_POST['AdminhtmlAdmin']['create_time']= date('Y-m-d H:i:s');
			$model->attributes=$_POST['AdminhtmlAdmin'];
			if($model->save()){
				Yii::app()->admin->putSuccessMsg('成功保存信息！');
				$this->redirect(array('view','id'=>$model->admin_id));
			}
		}

		$this->render('create',array( 'model'=>$model, ));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$this->setUnionTitle('更新信息#'. $id);
		$model=$this->loadModel($id);
		$this->attachLogBehavior($model);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		
		if(isset($_POST['AdminhtmlAdmin']))
		{
			if($_POST['AdminhtmlAdmin']['password']!= $_POST['AdminhtmlAdmin']['password_cf']) {
				Yii::app()->admin->putErrorMsg('两次密码输入不一致！');
				
			} else {
				$_POST['AdminhtmlAdmin']['password']= md5($_POST['AdminhtmlAdmin']['password']);
				$model->attributes=$_POST['AdminhtmlAdmin'];
				if($model->save()){
					Yii::app()->admin->putSuccessMsg('成功保存信息！');
			    }
			}
		}
		$model->setAttribute('password', '******');
		$this->render('update',array( 'model'=>$model, ));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$model= $this->loadModel($id);
			$this->attachLogBehavior($model);
			$model->delete();
			
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$this->setUnionTitle('管理员列表');
		$dataProvider=new CActiveDataProvider('AdminhtmlAdmin');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$this->setUnionTitle('管理列表');
		$model=new AdminhtmlAdmin('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['AdminhtmlAdmin']))
			$model->attributes=$_GET['AdminhtmlAdmin'];

		$this->render('admin',array(
			'model'=>$model->with('role'),
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=AdminhtmlAdmin::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='adminhtml-admin-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
